package bootstrap

import (
	"blog/app"
	autoload2 "blog/bootstrap/autoload"
	config2 "blog/config"
	"blog/core"
	"blog/core/cache"
	"blog/core/cron"
	"github.com/golobby/config/v3"
	"github.com/golobby/config/v3/pkg/feeder"
	"os"
)

type start struct {
}

var Bootstrap = new(start)

func (rev *start) Init() {
	autoload := []autoload2.Contract{new(autoload2.Redis), new(autoload2.Db)}
	rev.loadConfig()
	for _, v := range autoload {
		v.Init()
	}

	core.Cache = cache.New(core.Redis)
	cron.New().Load(app.Cron.Init()).Start()
	rev.loadRouter()
}

func (rev *start) loadConfig() {
	if _, err := os.Stat(core.ConfigPathName); err != nil {
		panic("配置文件不存在")
	}

	core.Config = &config2.Config{}
	env := feeder.DotEnv{
		Path: core.ConfigPathName,
	}

	config.New().AddFeeder(env).AddStruct(core.Config).Feed()
}

func (rev *start) loadRouter() {
	var initRouter = new(router)
	initRouter.Init()
}
